package org.iteam.core.service.sys;

import java.sql.SQLException;
import java.util.List;

import org.iteam.core.Constant;
import org.iteam.core.Constant.Status;
import org.iteam.core.model.sys.SysRole;
import org.iteam.core.service.BaseService;
import org.iteam.core.service.BaseServiceInterface;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.core.view.TreeGridNode;
import org.iteam.core.view.sys.PermissionView;

public abstract class SysRoleService extends BaseService<SysRole> implements
		BaseServiceInterface<SysRole> {
	private static SysRoleService serviceMysql = null;
	private static SysRoleService serviceOracle = null;

	public SysRoleService() {
		super(SysRole.class);
		// TODO Auto-generated constructor stub
	}

	public static SysRoleService getInstance() {
		Constant.DataBaseType dbTypes = Constant.dataBaseType;
		if (dbTypes.equals(Constant.DataBaseType.MYSQL)) {
			return getMySqlInstance();
		} else if (dbTypes.equals(Constant.DataBaseType.ORACLE)) {
			return getOracleInstance();
		}
		return null;
	}

	public List<SysRole> getRolesByUser(String account) throws SQLException {
		String sql = "SELECT A.AUTH_ROLE AS CODE, B. NAME, B.ALIAS, A.AUTH_USER, A.PRI FROM SYS_AUTH A LEFT JOIN SYS_ROLE B ON A.AUTH_ROLE = B. CODE WHERE B. STATUS = ? AND A.AUTH_USER = ? ORDER BY A.PRI DESC";
		return super.query(sql, Status.ENABLE.ecode, account);
	}

	public static SysRoleService getMySqlInstance() {
		if (serviceMysql == null)
			serviceMysql = new org.iteam.core.service.mysql.impl.sys.SysRoleServiceImpl();
		return serviceMysql;
	}

	public static SysRoleService getOracleInstance() {
		if (serviceOracle == null)
			serviceOracle = new org.iteam.core.service.oracle.impl.sys.SysRoleServiceImpl();
		return serviceOracle;
	}

	public abstract List<PermissionView> querySub(long page, long rows,
			String role) throws SQLException;

	public abstract long countSub(String role) throws SQLException;

	public abstract boolean updateRoleFunctions(String role, String _module,
			boolean _permission, int _type) throws SQLException;

	@Override
	public List<SysRole> query() throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysRole.TABLE_NAME, "*");
		return super.query(sb.getSql().toUpperCase());
	}

	@Override
	public long count() throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysRole.TABLE_NAME, "*");
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase())
				.toString());
	}

	public long validate(String code) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysRole.TABLE_NAME, "*");
		sb.addWhere("code", code);
		return Long.parseLong(super.certain(sb.getMaxCountSql().toUpperCase(),
				code).toString());
	}

	@Override
	public SysRole show(String id) throws SQLException {
		SqlBuilder sb = SqlBuilder.createQuery(SysRole.TABLE_NAME, "*");
		sb.addWhere("code", id);
		return super.show(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean update(SysRole obj) throws SQLException {
		SqlBuilder sb = SqlBuilder.createUpdate(SysRole.TABLE_NAME);
		sb.addField("name", obj.getName());
		sb.addField("alias", obj.getAlias());
		sb.addField("status", obj.getStatus());
		sb.addWhere("code", obj.getCode());
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean delete(String id) throws SQLException {
		SqlBuilder sb = SqlBuilder.createUpdate(SysRole.TABLE_NAME);
		sb.addField("status", Status.DELETE.ecode);
		sb.addWhere("code", id);
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean destroy(String id) throws SQLException {
		SqlBuilder sb = SqlBuilder.createDelete(SysRole.TABLE_NAME);
		sb.addWhere("code", id);
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	public abstract List<TreeGridNode> getGridNodes(String role);
}
